package com.dequeue;

import java.io.Serializable;

import com.lists.DoubleList;
import com.lists.EmptyListException;

public class DeQueueList implements Dequeue, Serializable {

	private int contador;
	private DoubleList listaDoble;

	public static DeQueueList newInstance(DeQueueList dl) {
		return new DeQueueList(dl.size(), dl.getListaDoble());
	}

	public DeQueueList(int contador, DoubleList listaDoble){
		this.contador = contador;
		this.listaDoble = listaDoble;
	}
	
	public DoubleList getListaDoble() {
		return listaDoble;
	}

	public DeQueueList() {
		// TODO Auto-generated constructor stub
		contador = 0;
		listaDoble = new DoubleList();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return contador;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return listaDoble.isEmpty();
	}

	@Override
	public void insertFirst(Object object) {
		// TODO Auto-generated method stub
		listaDoble.insertAtFront(object);
		contador++;
	}

	@Override
	public void insertLast(Object object) {
		// TODO Auto-generated method stub
		listaDoble.insertAtLast(object);
		contador++;
	}

	@Override
	public Object removeFromFirst() throws DeQueueEmptyException {
		// TODO Auto-generated method stub
		try {
			Object objeto = listaDoble.removeFromFront();
			contador--;
			return objeto;
		} catch (EmptyListException e) {
			throw new DeQueueEmptyException();
		}
	}

	@Override
	public Object removeFromLast() throws DeQueueEmptyException {
		try {
			Object objeto = listaDoble.removeFromBack();
			contador--;
			return objeto;
		} catch (EmptyListException e) {
			throw new DeQueueEmptyException();
		}
	}

	@Override
	public Object first() throws DeQueueEmptyException {
		// TODO Auto-generated method stub
		return listaDoble.getFirstNode().getData();
	}

	@Override
	public Object last() throws DeQueueEmptyException {
		// TODO Auto-generated method stub
		return listaDoble.getLastNode().getData();
	}

	public static void main(String args[]) {

		DeQueueList deque = new DeQueueList();

		deque.insertFirst(40);
		deque.insertFirst(10);
		deque.insertFirst(80);
		deque.insertLast(70);
		deque.insertLast(50);
		deque.insertLast(70);
		deque.insertFirst(10);

		while (deque.size() > 0) {
			System.out.println(deque.removeFromFirst());
		}

	}

}
